package pt.isec.gpsgroup5.moneybox;

import java.math.BigDecimal;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;

public class CheckInAuto extends Activity {

	private DBAdapter db;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_check_in_auto);

		db = new DBAdapter(this);
		db.open(this);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_check_in_auto, menu);
		return true;
	}

	public void onSubmit(View v) {

		EditText newObjectiveFinalAmount = (EditText) findViewById(R.id.autoCheckIn);
		double amount = 0;
		double amountSec = (double) 0;
		
		AlertDialog.Builder bdialog = new AlertDialog.Builder(this);
		bdialog.setTitle("");
		bdialog.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int which) {
				finish();
			}
		});

		try{
			amountSec = Double.parseDouble(newObjectiveFinalAmount.getText().toString());
		}catch(Exception e){
			bdialog.setMessage("Not a valid value");
			AlertDialog rdialog = bdialog.create();
			rdialog.show();
		}
		

		Double soma;
		Double soma1, soma2, soma3;
		int numObj = db.numObjectives();
		int priority;
		Objective obj;

		
		while (true) {

			amount = amountSec;
			amountSec = 0;
			soma = (double) 0;

			for (int i = 0; i < numObj; i++) {

				obj = db.getObjectivePos(i);
				
				if(obj.getCurrentAmount() == obj.getFinalAmount())
					continue;

				priority = obj.getPriority();
				switch (priority) {
				case 3:
					soma += 3;
					break;
				case 2:
					soma += 2;
					break;
				case 1:
					soma += 1;
				}
			}

			soma3 = (amount * 3) / soma;
			soma2 = (amount * 2) / soma;
			soma1 = amount / soma;

			for (int i = 0; i < numObj; i++) {
				obj = db.getObjectivePos(i);
				
				if(obj.getCurrentAmount() == obj.getFinalAmount())
					continue;

				priority = obj.getPriority();
				switch (priority) {
				
				case 3:
					if (obj.getCurrentAmount() + soma3 > obj.getFinalAmount()) {
						amountSec += soma3 - (obj.getFinalAmount() - obj.getCurrentAmount());
						obj.setCurrentAmount(obj.getFinalAmount());
					} else {
						obj.setCurrentAmount(new BigDecimal(obj.getCurrentAmount() + soma3).setScale(2,BigDecimal.ROUND_HALF_UP).doubleValue());
					}
					break;

				case 2:
					if (obj.getCurrentAmount() + soma2 > obj.getFinalAmount()) {
						amountSec += soma2 - (obj.getFinalAmount() - obj.getCurrentAmount());
						obj.setCurrentAmount(obj.getFinalAmount());
					} else {
						obj.setCurrentAmount(new BigDecimal(obj.getCurrentAmount() + soma2).setScale(2,BigDecimal.ROUND_HALF_UP).doubleValue());
					}
					break;
					
				case 1:
					if (obj.getCurrentAmount() + soma1 > obj.getFinalAmount()) {
						amountSec += soma1 - (obj.getFinalAmount() - obj.getCurrentAmount());
						obj.setCurrentAmount(obj.getFinalAmount());
					} else {
						obj.setCurrentAmount(new BigDecimal(obj.getCurrentAmount() + soma1).setScale(2,BigDecimal.ROUND_HALF_UP).doubleValue());
					}
					break;
				}
				
				db.updateObjective(obj, obj.getId());
			}
			if (amountSec == 0) {
				bdialog.setMessage(R.string.checkinAutoComplete);
				AlertDialog dialog = bdialog.create();
				dialog.show();
				break;
			} else if (fullComplete()) {
				bdialog.setMessage(R.string.checkinFullComplete);
				AlertDialog dialog = bdialog.create();
				dialog.show();
				break;
			}
		}
	}

	public boolean fullComplete() {
		Objective obj = null;
		for (int i = 0; i < db.numObjectives(); i++) {
			obj = db.getObjectivePos(i);
			if (obj.getCurrentAmount() != obj.getFinalAmount()) {
				return false;
			}
		}
		return true;
	}
}

